package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Phones;
import com.ruoyi.system.service.IPhonesService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 机型库Controller
 * 
 * @author ruoyi
 * @date 2024-12-12
 */
@Controller
@RequestMapping("/system/phones")
public class PhonesController extends BaseController
{
    private String prefix = "system/phones";

    @Autowired
    private IPhonesService phonesService;

    @RequiresPermissions("system:phones:view")
    @GetMapping()
    public String phones()
    {
        return prefix + "/phones";
    }

    /**
     * 查询机型库列表
     */
    @RequiresPermissions("system:phones:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Phones phones)
    {
        startPage();
        List<Phones> list = phonesService.selectPhonesList(phones);
        return getDataTable(list);
    }

    /**
     * 导出机型库列表
     */
    @RequiresPermissions("system:phones:export")
    @Log(title = "机型库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Phones phones)
    {
        List<Phones> list = phonesService.selectPhonesList(phones);
        ExcelUtil<Phones> util = new ExcelUtil<Phones>(Phones.class);
        return util.exportExcel(list, "机型库数据");
    }

    /**
     * 新增机型库
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }
    @GetMapping("/flushPhone")
    public String flushPhone()
    {
        return prefix + "/flushPhone";
    }



    /**
     * 新增机型库
     */
    @PostMapping("/flushPhone")
    public AjaxResult flushPhone(Phones phones)
    {
        System.out.println("flushPhone");
        System.out.println(phones.getIps());
        return null;
    }

    /**
     * 新增保存机型库
     */
    @RequiresPermissions("system:phones:add")
    @Log(title = "机型库", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Phones phones)
    {
        return toAjax(phonesService.insertPhones(phones));
    }

    /**
     * 修改机型库
     */
    @RequiresPermissions("system:phones:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Phones phones = phonesService.selectPhonesById(id);
        mmap.put("phones", phones);
        return prefix + "/edit";
    }

    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<Phones> util = new ExcelUtil<Phones>(Phones.class);
        List<Phones> phoneList = util.importExcel(file.getInputStream());
        String message = phonesService.importPhone(phoneList, updateSupport, getLoginName());
        return AjaxResult.success(message);
    }

    /**
     * 修改保存机型库
     */
    @RequiresPermissions("system:phones:edit")
    @Log(title = "机型库", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Phones phones)
    {
        return toAjax(phonesService.updatePhones(phones));
    }

    /**
     * 删除机型库
     */
    @RequiresPermissions("system:phones:remove")
    @Log(title = "机型库", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(phonesService.deletePhonesByIds(ids));
    }
}
